package com.exam.paper.controller;

import com.exam.common.entity.PageRequest;
import com.exam.common.entity.Request;
import com.exam.common.entity.StatusCode;
import com.exam.paper.pojo.Paper;
import com.exam.paper.service.PaperService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName : ExamController
 * @Description : 试卷
 * @Author : WHB
 */

@RestController
@RequestMapping("exam")
@Api(description = "试卷信息API接口")
public class PaperController {

    @Autowired
    PaperService paperService;

    /**
     * 分页查询试卷信息
     * @param page
     * @param size
     * @return
     */
    @GetMapping("pagelist/{page}/{size}")
    @ApiOperation(value="分页查询试卷信息", notes="分页查询试卷信息")
    public ResponseEntity<PageRequest<Paper>> queryCategoryByPage(
            @PathVariable("page") Integer page,
            @PathVariable("size") Integer size,
            @RequestParam(value="id",required = false,defaultValue = "666") Long id
    ){
        return ResponseEntity.ok(paperService.queryExamByPage(page,size,id));
    }

    @GetMapping("pagelist")
    @ApiOperation(value="通过用户id查询试卷信息", notes="通过用户id查询试卷信息")
    public Request queryPageList(
            @RequestParam("id") Long id
    ){
        return new Request(true,StatusCode.OK,"通过用户id查询试卷成功",paperService.queryPageList(id));
    }

    @GetMapping("pagelist/{categoryId}")
    @ApiOperation(value="通过分类id查询试卷信息", notes="通过分类id查询试卷信息")
    public Request queryPageListByCategoryId(@PathVariable("categoryId") Long cid){
        return new Request(true,StatusCode.OK,"通过用户id查询试卷成功",paperService.queryPageListByCategoryId(cid));
    }

    /**
     * 条件查询试卷信息
     * @param page
     * @param size
     * @return
     */
    @GetMapping("searchlist/{page}/{size}/{cid}")
    @ApiOperation(value="分页条件查询试卷信息", notes="分页条件查询试卷信息")
    public ResponseEntity<PageRequest<Paper>> searchList(
            @PathVariable("page") Integer page,
            @PathVariable("size") Integer size,
            @PathVariable("cid") Long cid,
            @RequestParam(value="id",required = false,defaultValue = "666") Long id
    ){
        return ResponseEntity.ok(paperService.searchList(page,size,cid,id));
    }

    /**
     * 添加试卷
     * @param data
     * @return
     */
    @PostMapping("exam")
    @ApiOperation(value="添加试卷", notes="添加试卷")
    public Request addPaper(@RequestBody String data){
        try {
            paperService.addPaper(data);
            return new Request(true, StatusCode.OK,"新增试卷成功");
        }catch (Exception e){
            System.out.println(e);
            return new Request(false, StatusCode.ERROR,"新增试卷失败");
        }
    }

    /**
     * 用户添加试卷
     * @param data
     * @return
     */
    @PostMapping("user/{id}")
    @ApiOperation(value="添加试卷", notes="添加试卷")
    public Request addPaper(@RequestBody String data,@PathVariable("id") Long creatId){
        try {
            paperService.adduserPaper(data,creatId);
            return new Request(true, StatusCode.OK,"新增试卷成功");
        }catch (Exception e){
            System.out.println(e);
            return new Request(false, StatusCode.ERROR,"新增试卷失败");
        }
    }

    /**
     * 修改试卷
     * @param data
     * @return
     */
    @PutMapping("exam")
    @ApiOperation(value="修改试卷", notes="修改试卷")
    public Request editPaper(@RequestBody String data){
        try {
            paperService.editPaper(data);
            return new Request(true, StatusCode.OK,"修改试卷成功");
        }catch (Exception e){
            System.out.println(e);
            return new Request(false, StatusCode.ERROR,"修改试卷失败");
        }
    }

    @GetMapping("{id}")
    @ApiOperation(value="通过id查询试卷", notes="通过id查询试卷")
    public Request queryPaperById(@PathVariable("id") Long id){

            return new Request(true, StatusCode.OK,"查询试卷成功",paperService.queryPaperById(id));
    }

    @PostMapping("evaluation")
    @ApiOperation(value="试卷评分", notes="试卷评分")
    public Request evaluation(@RequestBody String data){
        return new Request(true,StatusCode.OK,"评分成功",paperService.evaluation(data));
    }

    @GetMapping("testScore")
    @ApiOperation(value="成绩", notes="成绩")
    public int getScore(@RequestParam("testId") Integer testId,@RequestParam("userId") Long userId){
        return paperService.getScore(testId,userId);
    }
}
